const router = require('koa-router')();

/**
 * 获取单个模板By id
 */
router.get('/', httpResult.resp (async ctx => {
  let admin = ctx.session.admin;
  let id = ctx.query.id;
  ctx.assert(id != undefined, 'missing query param : id');
  let query = {_id : id };
  let q = _.assign(query, {customer_chain: new RegExp(`^${admin.customer_chain}`)});
  let template =  await models.messageTemplate.findOne(q, {customer_chain : 0, customer_name : 0})
      .populate('suggestions')
      .populate('media')
      .populate('message_card_list').lean({virtuals : true});
  if(template.suggestions && template.suggestions._id){
    template.suggestions.menus = await models.suggestion.find({media_id : template.suggestions._id}).lean();
  }
  if(template.message_card_list && template.message_card_list.length > 0){
    let cardsList = await models.messageCard.find({_id : {$in : template.message_card_list}}).populate('media').populate('suggestions');
    template.message_card_list = cardsList;
  }
  return template;
}));
/**
 * 获取消息模板列表
 */
router.get('/list', httpResult.resp (async ctx => {
  let admin = ctx.session.admin;
  let query = ctx.query;
  if(query.name){
    query.name = new RegExp(query.name);
  }
  let q = _.assign(query, {customer_chain: new RegExp(`^${admin.customer_chain}`)});
  return await models.messageTemplate.find(q, {customer_chain : 0, customer_name : 0}).populate('media');
}));

router.post('/', httpResult.resp(async ctx => {
  let admin = ctx.session.admin;
  let data = ctx.request.body;
  ctx.assert(data.type !== undefined, 'missing params: type');
  ctx.assert(data.name, 'missing params: name');
console.log(data);
  let info = {
    name : data.name,
    type : data.type,
    tags : data.tags,
    suggestions : data.suggestions || undefined,
    customer_name   : admin.customer_name,
    customer_chain  : admin.customer_chain,
    creator         : admin._id,  //对应adminUserModel 的objectId  //除超级管理员外，都有creator
    update_time     : new Date(),//upload time
  };
  switch(data.type){
    case 0 : {
      ctx.assert(data.text, 'missing params: text');
      info.text = data.text;
      break;
    }
    case 1 : {
      ctx.assert(data.latitude, 'missing params: latitude');
      ctx.assert(data.longitude, 'missing params: longitude');
      ctx.assert(data.label, 'missing params: label');
      info.latitude = data.latitude;
      info.longitude = data.longitude;
      info.label = data.label;
      break;
    }
    case 2 : {
      ctx.assert(data.media, 'missing params: media');
      info.media = data.media;
      break;
    }
    case 3 : {
      ctx.assert(data.message_card_list && data.message_card_list.length === 1, 'error params: message_card_list');
      info.message_card_list = data.message_card_list;
      break;
    }
    case 4 : {
      ctx.assert(data.message_card_list && data.message_card_list.length >= 2, 'error params: message_card_list');
      info.message_card_list = data.message_card_list;
      break;
    }
  }
  console.log(info);
  let result;
  if(data._id){
    result = await models.messageTemplate.findOneAndUpdate({_id : data._id}, {$set : info}, {returnNewDocument : true});
  }else{
    result = await models.messageTemplate.create(info);
  }
  return result
}));

router.del('/', httpResult.resp(async ctx => {
  let admin = ctx.session.admin;
  let data = ctx.request.body;

  ctx.assert(data.id, 'missing params: id');

  return await models.messageTemplate.findOneAndDelete({_id : data.id});
}));

module.exports = router;
